import java.io.*;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 创建一个线程池
 */


public class ThreadPoolCpoy implements Callable {

    @Override
    public Object call() throws Exception {

        //完成文件的复制
        File src = new File(".\\src\\1");
        File des = new File(".\\src\\2");
        copy(src,des);

        //删除文件
        File f2 = new File(".\\src\\2\\1.2");
        if(f2.exists()){
            deleteAll(f2);
        }


        return null;
    }

    public static void deleteAll(File src){
        if (!src.isDirectory()){
            src.delete();
        }else {
            File[] deleteArray = src.listFiles();
            //空文件夹
            if(deleteArray.length == 0){
                src.delete();
                return;
            }
            //删除子文件或子文件夹
            for (File src1:deleteArray){
                if(src1.isDirectory()){
                    deleteAll(src1);
                }else{
                    src1.delete();
                }
            }
            //最后删除文件夹本身
            src.delete();
        }
    }

    //文件复制函数，如果遍历到文件夹，在目标目录下创建一个同名文件夹，如果遍历到文件，调用write函数复制文件
    public static void copy(File src,File des){
        File[] fileArray = src.listFiles();
        if (fileArray != null) {
            for (File src1: fileArray) {
                File des1 = new File(des,src1.getName());
                if(src1.isDirectory()){
                    des1.mkdir();
                    copy(src1,des1);
                }else {
                    write(src1,des1);
                }
            }
        }

    }

    //write函数通过输入输出流复制文件
    public static void write(File src,File des){
        //创建输入输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(des);
            byte[] arr = new byte[1024];
            int res = 0;
            while((res = fis.read(arr)) != -1){
                fos.write(arr,0,res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        //1.创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        ThreadPoolCpoy tp = new ThreadPoolCpoy();
        //FutureTask fk = new FutureTask(tp);
        //2.向线程池中布置任务
        executorService.submit(tp);
        try {
            Object obj = tp.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //3.关闭线程池
        executorService.shutdown();
    }
}
